<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar @click-left="toMy" left-arrow title="编辑资料"></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
      <!-- 上传组件 -->
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        @click="showNameDialog"
        title="名称"
        :value="userInfo.name"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender === 0 ? '男' : '女'"
        @click="showGender = true"
      />
      <van-cell
        @click="showBirthdayPop"
        is-link
        title="生日"
        :value="userInfo.birthday"
      />
      <!-- 姓名编译框 -->
      <van-dialog
        @confirm="saveName"
        v-model="showName"
        title="修改昵称"
        show-cancel-button
      >
        <van-field v-model="name" type="text" ref="nameField" />
      </van-dialog>
      <!-- 性别编译框 -->
      <van-popup v-model="showGender" position="bottom">
        <!-- 顶部导航栏 -->
        <van-nav-bar
          title="修改性别"
          left-text="取消"
          @click-left="showGender = false"
        />
        <!-- 性别选择区 -->
        <van-cell-group>
          <van-cell title="男" is-link @click="changeGender(0)" />
          <van-cell title="女" is-link @click="changeGender(1)" />
        </van-cell-group>
      </van-popup>
      <!-- 出生日期选择 -->
      <!-- 生日选择区域 -->
      <van-popup position="bottom" v-model="showBirthday">
        <van-datetime-picker
          v-model="birthday"
          type="date"
          title="选择年月日"
          @cancel="cancelDate"
          @confirm="confirmDate"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
      <!-- 裁切区域弹框 -->
      <div class="cropper-box" v-show="isShowCropper">
        <!-- 裁切组件 -->
        <vueCropper
          ref="cropper"
          fixed
          autoCropWidth="120"
          autoCropHeight="120"
          autoCrop
          :img="img"
        ></vueCropper>
        <!-- 2个按钮 -->
        <van-button @click="submitPhoto" class="confirm-btn" type="primary"
          >确认</van-button
        >
        <van-button
          @click="isShowCropper = false"
          class="cancel-btn"
          type="info"
          >取消</van-button
        >
      </div>
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo, uploadPhoto } from '../../api/edit'
import { VueCropper } from 'vue-cropper'
import moment from 'moment'
export default {
  name: 'edit',
  data () {
    return {
      showName: false, // 是否显示姓名编辑框
      name: '', //  用户的昵称
      showGender: false, // 性别编辑
      showBirthday: false, // 是否显示生日弹框
      // 最小值
      minDate: new Date(1970, 0, 1),
      // 最大值
      maxDate: new Date(2100, 10, 1),
      // 当前选中的日期
      birthday: new Date(1989, 10, 17),
      // 裁切的图片
      img: '',
      isShowCropper: false
    }
  },
  components: {
    VueCropper
  },
  methods: {
    toMy () {
      this.$router.push({ path: '/my' })
    },
    showNameDialog () {
      // 显示弹框
      this.showName = true
      this.name = this.userInfo.name

      // 自动获取焦点
      this.$nextTick(() => {
        this.$refs.nameField.focus()
      })
    },
    saveName () {
      // console.log('serve')
      editUserInfo({ name: this.name }).then(res => {
        console.log(res)
        this.$toast.success('更新成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: res.data.data.name
        })
      })
    },
    // 修改性别
    changeGender (gender) {
      editUserInfo({ gender }).then(res => {
        // console.log(res)
        this.$toast.success('性别更新成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender
        })
        // 关闭弹框
        this.showGender = false
      })
    },
    // 弹出生日编辑框
    showBirthdayPop () {
      this.showBirthday = true
      // 设置用户的生日为默认值
      this.birthday = new Date(this.userInfo.birthday)
    },
    // 日期选择器,点击确认
    confirmDate () {
      // console.log('confirmDate')
      // console.log(this.birthday)
      // 获取选择的日期
      // 需求的格式是2000-01-01
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.$toast.success('更新成功')
        this.showBirthday = false

        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: res.data.data.birthday
        })
      })
    },
    cancelDate () {
      this.showBirthday = false
    },
    // 选择文件之后执行的回调函数
    afterRead (file) {
      // 此时可以自行将文件上次至服务器
      console.log(file)
      // 展示裁切框
      this.isShowCropper = true
      // 设置裁切的图片,base64的编码
      this.img = file.content
      // const data = new FormData()
      // data.append('photo', file.file)
      // uploadPhoto(data).then(res => {
      //   // console.log(res)
      //   this.$toast.success('更新成功')
      //   this.$store.commit('setUserInfo', {
      //     ...this.userInfo,
      //     photo: res.data.data.photo
      //   })
      // })
    },
    // 裁切之后, 上传图片
    submitPhoto () {
      // 获取blob这种格式
      // formDdata+接口提交到远程
      this.$refs.cropper.getCropBlob(blob => {
        const data = new FormData()
        data.append('photo', blob)
        uploadPhoto(data).then(res => {
          // console.log(res)
          this.$toast.success('更新成功')
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          // 关闭切图界面
          this.isShowCropper = false
        })
      })
    }
  },
  computed: mapState(['userInfo'])
  // computed: {
  //   userInfo () {
  //     return this.$store.state.userInfo
  //   }
  // }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  // .van-nav-bar {
  //   background-color: #3196fa;
  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  //   .van-icon-arrow-left {
  //     color: #fff;
  //   }
  // }
  // 头像部分
  .avatar {
    position: relative;
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
    .van-uploader {
      // 上传组件的样式
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      width: 120px;
      height: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
  // 编译框
  .van-dialog {
    padding: 10px;
  }
  .van-field {
    border: 1px solid #000;
    border-radius: 5px;
  }

  // 裁切弹框样式
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 90%;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
